/**
 * 定义PLC点位配置模块
 */
(function(window, angular, undefined){
    var module = angular.module('water.apps.sw.bcPlcPointConfig.bcPlcPointConfigManager', ['water.entity.bcPlcPointConfig',
        'water.entity.protocol']);

    module.controller('bcPlcPointConfigManagerController',function($scope,$bcPlcPointConfigService
                                                            ,$bcMonitorParamDictService){
        /**
         * 数据列表
         */
        $scope.dataList;

        /**
         * 用于新建的model
         */
        $scope.addModel;
        /**
         * 用于编辑的model
         */
        $scope.editModel;

        /**
         * 是否选择集
         */
        $scope.whetherSelections = [{"k":"0","v":"是"},
            {"k":"1","v":"否"}];

        /**
         * plc点位数据类型选择集
         */
        $scope.pointDataTypeSelections = [{"k":"float","v":"float"},
            {"k":"int","v":"int"},
            {"k":"bit","v":"bit"},
            {"k":"date","v":"date"}
        ];

        /**
         * 打开新增窗口按钮点击事件
         */
        $scope.onAddClick = function(){
            $scope.addModel = {};
            $scope.addDialog.open();
        }

        $scope.onCloseAddClick = function(){
            $scope.addDialog.close();
        }

        /**
         * 保存按钮点击事件
         */
        $scope.onSaveClick = function(model){
            $bcPlcPointConfigService.create(model).then(function(){
                alert("保存成功");
                $scope.onCloseAddClick();
                $scope.getList();
            }).catch(function(error){
                alert(error);
            });
        }

        /**
         * 打开编辑窗口按钮点击事件
         */
        $scope.onEditClick = function(model){
            $scope.editModel = angular.copy(model);
            $scope.editDialog.open();
        }

        $scope.onCloseEditClick = function(){
            $scope.editDialog.close();
        }

        /**
         * 更新按钮点击事件
         */
        $scope.onUpdateClick = function(model){
            $bcPlcPointConfigService.update(model).then(function(){
                alert("保存成功");
                $scope.onCloseEditClick();
                $scope.getList();
            }).catch(function(error){
                alert(error);
            });
        }

        /**
         * 删除按钮点击事件
         */
        $scope.onDeleteClick = function(id){
            $bcPlcPointConfigService.deleteModel(id).then(function(){
                alert("删除成功");
                $scope.getList();
            });
        }

        $scope.getList = function(){
            $bcPlcPointConfigService.getList().then(function(data){
                data.forEach(function (data) {
                    /**
                     * 监测项名翻译
                     */
                    $scope.paramSelections.forEach(function (item) {
                        if(data.PARAM_CODE_BUS===item.k){
                            data.PARAM_NAME = item.v;
                        }
                    });

                    /**
                     * PLC点位数据类型翻译
                     */
                    $scope.pointDataTypeSelections.forEach(function (item) {
                        if(data.POINT_DATA_TYPE===item.k){
                            data.POINT_DATA_TYPE_SHOW = item.v;
                        }
                    });

                    $scope.whetherSelections.forEach(function (item) {
                        /**
                         * 是否展示在主界面翻译
                         */
                        if(data.IS_DISPLAY_MAIN===item.k){
                            data.IS_DISPLAY_MAIN_SHOW = item.v;
                        }
                        /**
                         * 是否展示在详情界面翻译
                         */
                        if(data.IS_DISPLAY_DETAIL===item.k){
                            data.IS_DISPLAY_DETAIL_SHOW = item.v;
                        }
                        /**
                         * 是否组合展示翻译
                         */
                        if(data.IS_COMBO_DISPLAY===item.k){
                            data.IS_COMBO_DISPLAY_SHOW = item.v;
                        }
                        /**
                         * 是否报警点位翻译
                         */
                        if(data.IS_ALARM_POINT===item.k){
                            data.IS_ALARM_POINT_SHOW = item.v;
                        }
                        /**
                         * 是否测量值翻译
                         */
                        if(data.IS_MEAS_VALUE===item.k){
                            data.IS_MEAS_VALUE_SHOW = item.v;
                        }
                    });

                });

                $scope.dataList = data;
            });
        }

        $scope._init = function(){
            $bcMonitorParamDictService.getList().then(function (data) {
                var paramData = [];

                for (var i = 0; i < data.length; i++) {
                    paramData.push({
                        k: data[i].PARAM_CODE_BUS,
                        v: data[i].PARAM_NAME
                    });
                }
                $scope.paramSelections = paramData;
                $scope.plcParamDataModel.setData($scope.paramSelections);
                $scope.editPlcParamDataModel.setData($scope.paramSelections);
                $scope.getList();
            });
        }

        $scope._init();
    });

}(window, angular));


